﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
   public class ResourceStatDao : IDaoInterface<ResourceStat, ResourceStatCriteria>
   {
       #region  Variable & constant
       #endregion  Variable & constant

       #region Constructor & Destructor
       #endregion Constructor & Destructor

       #region Properties
       #endregion Properties

       #region Public Methods
       #region  IDaoInterface<ResourceStat, ResourceStatCriteria> Members
       public bool Insert(ResourceStat ResourceStatEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (ResourceStatEntity.EntityState == EntityState.New)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Dasideen_ResourceStat_Insert";
                   //CmdObj.CommandText = UserManagementSQLCommandText.InsertUser();
                   CmdObj.CommandType = CommandType.StoredProcedure;
                   //CmdObj.CommandType = CommandType.Text;

                   DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceKey", ParameterDirection.Input, DbType.Int32, ResourceStatEntity.ResourceKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceDescription", ParameterDirection.Input, DbType.String, ResourceStatEntity.ResourceDescription);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceTotalDownloads", ParameterDirection.Input, DbType.Int32, ResourceStatEntity.ResourceTotalDownloads);


                   DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ResourceStatEntity);


                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {

                       //ResourceStatEntity.ResourceKey = Convert.ToInt32(CmdObj.Parameters["@ResourceKey"].Value);
                       ResourceStatEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                       ResourceStatEntity.Sys_DateOfModification = ResourceStatEntity.Sys_DateOfCreation;
                       ResourceStatEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }
                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For inserting new ResourceStat entity data to database, [EntityState] attribute should be [New]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Update(ResourceStat ResourceStatEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (ResourceStatEntity.EntityState == EntityState.Changed)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Dasideen_ResourceStat_Update";
                   CmdObj.CommandType = CommandType.StoredProcedure;
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceKey", ParameterDirection.Input, DbType.Int32, ResourceStatEntity.ResourceKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceDescription", ParameterDirection.Input, DbType.String, ResourceStatEntity.ResourceDescription);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceTotalDownloads", ParameterDirection.Input, DbType.Int32, ResourceStatEntity.ResourceTotalDownloads);

                   DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ResourceStatEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {
                       ResourceStatEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                       ResourceStatEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }

                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For Updating existing ResourceStat entity data to database, [EntityState] attribute should be [Changed]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Delete(ResourceStat DataEntity)
       {
           throw new NotImplementedException();
       }

       public ResourceStat GetByID(string ID)
       {
           throw new NotImplementedException();
       }

       public List<ResourceStat> GetAll()
       {
           throw new NotImplementedException();
       }

       public List<ResourceStat> GetByCriteria(ResourceStatCriteria CriteriaEntity)
       {
           throw new NotImplementedException();
       }
       #endregion

       public ResourceStat GetByResourceKey(int ResourceKey)
       {
           ResourceStat _ResourceStatList = new ResourceStat(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Dasideen_ResourceStat_GetByKey";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@ResourceKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ResourceKey));


               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _ResourceStatList = new ResourceStat(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateResourceStatFieldsFromReaderToEntity(_ResourceStatList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No Resource found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _ResourceStatList;
       }

       #endregion Public Methods

       #region Events
       #endregion Events

       #region Private Methods
       private void TranslateResourceStatFieldsFromReaderToEntity(ResourceStat pResourceStat, DbDataReader pDbDataReader)
       {
           
               pResourceStat.ResourceKey = Convert.ToInt32(pDbDataReader["ResourceKey"]);
               pResourceStat.ResourceDescription = Convert.ToString(pDbDataReader["ResourceDescription"]);
               pResourceStat.ResourceTotalDownloads = Convert.ToInt32(pDbDataReader["ResourceTotalDownloads"]);

               DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pResourceStat, pDbDataReader);
           
       }
       #endregion Private Methods


   }
}
